<?php

namespace App\Listeners\OpenShop;

use App\Enums\Check\CheckEnum;
use App\Enums\OpenShop\InvestmentPromotion\MainProcessEnum;
use App\Enums\OpenShop\InvestmentPromotion\OrganizationManageEnum;
use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\InvestmentPromotion\SiteMissionEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Events\OpenShop\OpenShopProcessEvent;
use App\Events\OpenShop\OrderCreatedEvent;
use App\Library\FeiShu\SendFs;
use App\Listeners\BaseListener;
use App\Models\OpenShop\InvestmentPromotion\OpenShopLocationModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopOrganizationManageModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopProcessDetailNoticeEmployeeModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPayHistoryModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPermissionAnchorModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSiteMissionModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserBaseInfoModel;
use App\Models\OpenShop\ShopCheckAccept\OpenShopCheckAcceptModel;
use App\Models\OpenShop\ShopEngineeringConstruction\OpenShopEngineeringConstructionModel as EngineeringModel;
use App\Remote\Feishu\FeishuMsgRemote;
use App\Remote\Feishu\Tools\MsgTagBuild\ImgTagMsgBuild;
use App\Remote\Feishu\Tools\MsgTagBuild\TextTagMsgBuild;
use App\Remote\Feishu\Tools\TextMsgBuildTools;
use App\Remote\Rms\RmsUserRemote;
use App\Service\OpenShop\Feishu\NoticeService;
use App\Service\OpenShop\Feishu\OrganizationManageService;
use App\Service\OpenShop\Import\HistoryOrder\HistoryOrderParam;
use App\Service\OpenShop\User\RmsUserService;
use Illuminate\Support\Facades\Log;

class ProcessNoticeListener extends BaseListener
{

    protected OpenShopProcessEvent      $event;
    protected ?OpenShopSignOrderModel   $orderModel;
    protected ?OpenShopMainProcessModel $mainProcessModel;

    public function __construct()
    {
        parent::__construct();
    }

    public static function historyUidsByOrderNo(string $orderNo): array
    {
        $ids = [];
        // 判断是否为子订单
        $model = OpenShopMainProcessModel::query()
                                         ->with('childOrdersRel')
                                         ->where('orderNo', $orderNo)
                                         ->first();
        if (null === $model) {
            return $ids;
        }
        OpenShopProcessDetailNoticeEmployeeModel::query()
                                                ->whereIn('orderNo',
                                                    array_merge($model->getRelationsOrderNos(), [$orderNo]))
                                                ->get()
                                                ->map(function ($model) use (&$ids) {
                                                    $ids = array_merge($model->noticeEmployeeUids, $ids);
                                                });
        return array_unique($ids);
    }


    public function handle(OpenShopProcessEvent $event)
    {
        $this->event            = $event;
        $this->mainProcessModel = OpenShopMainProcessModel::query()
                                                          ->where('id', $event->mainProcessId)
                                                          ->first();
        if (null === $this->mainProcessModel) {
            return;
        }

        Log::channel('listener')
           ->info(__CLASS__ . " handle ProcessNoticeListener...orderNo: {$this->mainProcessModel->orderNo} in...");

        if (0 !== $this->mainProcessModel->childOrder) {
            // 非主订单不通知
            //return;
        }
        $this->orderModel = OpenShopSignOrderModel::query()
                                                  ->where('orderNo', $this->mainProcessModel->orderNo)
                                                  ->first();
        if (null === $this->orderModel) {
            return;
        }

        // 历史的所有的
        $noticeHistoryUids = $historyUids = static::historyUidsByOrderNo($this->orderModel->orderNo);

        switch ($this->event->mainProcessType) {
            case MainProcessEnum::MAIN_PROCESS_INVESTMENT_SIGN:
                switch ($this->event->processType) {
                    // 招商提交开店订单
                    case MainProcessEnum::INVESTMENT_SIGN_SUBMIT_SIGN_ORDER:
                        $historyUids[] = $this->orderModel->investmentManagerId;
                        break;
                    // 加盟商完成支付
                    case MainProcessEnum::INVESTMENT_SIGN_CONTRACT_PAID:
                        $historyUids[] = $this->orderModel->investmentManagerId;
                        break;
                    // 合同签署成功
                    case MainProcessEnum::INVESTMENT_SIGN_CONTRACT_SIGNED:
                        $historyUids[] = $this->orderModel->investmentManagerId;
                        break;
                }
                break;
            case MainProcessEnum::MAIN_PROCESS_SHOP_LOCATION:
                switch ($this->event->processType) {
                    // 指派开发经理
                    case MainProcessEnum::SHOP_LOCATION_ASSIGN_MANAGER:
                        $investmentManagerIds = OpenShopLocationModel::query()
                                                                     ->where("orderNo",
                                                                         $this->mainProcessModel->orderNo)
                                                                     ->where('step',
                                                                         MainProcessEnum::SHOP_LOCATION_ASSIGN_MANAGER)
                                                                     ->pluck('investmentManagerId')
                                                                     ->toArray();

                        $spaceDesignObj = OpenShopSpaceDesignModel::query()->where("orderNo", $this->mainProcessModel->orderNo)->first();
                        if($spaceDesignObj && $spaceDesignObj->preparationUid > 0){
                            //通知用户增加筹备顾问
                            $noticeHistoryUids = array_merge($noticeHistoryUids, [$spaceDesignObj->preparationUid]);
                            //权限增加筹备顾问
                            $signOrderPermissionAnchor = OpenShopSignOrderPermissionAnchorModel::query()->where("orderNo", $this->mainProcessModel->orderNo)->firstOrNew();
                            $signOrderPermissionAnchor->setRawAttributes([
                                "preparationUid" => $spaceDesignObj->preparationUid
                            ]);
                            $spaceDesignObj->save();
                            //历史通知用户增加筹备顾问
                            $historyUids       = array_merge($historyUids, [$spaceDesignObj->preparationUid]);
                        }

//                        //获取订单基础信息
//                        $userBaseInfoObj = OpenShopUserBaseInfoModel::getDataByOrderNo($this->mainProcessModel->orderNo);
//                        //获取筹备信息
//                        $organizationManageObj = OpenShopOrganizationManageModel::getOrganizationManageByProvinceCode($userBaseInfoObj->chooseShopProvinceCode);
//                        if ($organizationManageObj &&
//                            $organizationManageObj->organizationManageRel &&
//                            $organizationManageObj->organizationManageRel->preparationUid > 0) {
//
//                            $noticeHistoryUids = array_merge($noticeHistoryUids,
//                                [$organizationManageObj->organizationManageRel->preparationUid]);
//
//                            $historyUids       = array_merge($historyUids, [$organizationManageObj->organizationManageRel->preparationUid]);
//
//                            $signOrderPermissionAnchor = OpenShopSignOrderPermissionAnchorModel::query()->where("orderNo", $this->mainProcessModel->orderNo)->firstOrNew();
//                            $signOrderPermissionAnchor->setRawAttributes(["noticeUids" => implode(
//                                ",",
//                                array_merge(
//                                    array_filter(
//                                        explode(",", $signOrderPermissionAnchor->noticeUids)
//                                    ),
//                                    [$organizationManageObj->organizationManageRel->preparationUid]
//                                )
//                            )]);
//                            $signOrderPermissionAnchor->save();
//                        }


                        $historyUids          = array_merge($investmentManagerIds, $historyUids);
                        $historyUids[]        = $this->event->uid;
                        break;
                    // 开发选址表单提交
                    case MainProcessEnum::SHOP_LOCATION_FORM_SUBMIT:
                        $historyUids[] = $this->event->uid;
                        break;
                    // 加盟商确认开发选址地点
                    case MainProcessEnum::SHOP_LOCATION_CONFIRM:
                        // 历史数据中有开发经理数据，无需再指定
                        break;
                }
                break;
            case MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN:
                switch ($this->event->processType) {
                    case MainProcessEnum::SPACE_DESIGN_STEP_TYPE_CONFIRM_SHOP_INFO:
//                        //筹备确认
                        $spaceDescUids = OpenShopSpaceDesignModel::getUids($this->mainProcessModel->orderNo,
                            MainProcessEnum::SPACE_DESIGN_STEP_TYPE_CONFIRM_SHOP_INFO, 'preparationUid');
                        $historyUids   = array_merge($spaceDescUids, $historyUids);
                        $historyUids[] = $this->event->uid;
                        break;
                    case MainProcessEnum::SPACE_DESIGN_STEP_TYPE_SPACE_DESIGN_ASSIGN_WAIT:
                        //指定空间设计师
                        $spaceDescUids = OpenShopSpaceDesignModel::getUids($this->mainProcessModel->orderNo,
                            MainProcessEnum::SPACE_DESIGN_STEP_TYPE_SPACE_DESIGN_ASSIGN_WAIT, 'designUid');
                        $historyUids   = array_merge($spaceDescUids, $historyUids);
                        $historyUids[] = $this->event->uid;
                        break;
                    case MainProcessEnum::SPACE_DESIGN_STEP_TYPE_DESIGN_UPLOAD_FILE:
                        //上传设计图
                        $historyUids[] = $this->event->uid;
                        break;
                }
                break;
            case MainProcessEnum::MAIN_PROCESS_ENGINEER_CONSTRUCT:
                switch ($this->event->processType) {
                    case MainProcessEnum::ENGINEER_CONSTRUCT_START:
                        //筹备部确认施工进场
                        $engineeringUids = EngineeringModel::query()
                                                           ->where('orderNo', $this->mainProcessModel->orderNo)
                                                           ->where('step', MainProcessEnum::ENGINEER_CONSTRUCT_START)
                                                           ->pluck("updatedByUid")
                                                           ->toArray();
                        $historyUids     = array_merge($engineeringUids, $historyUids);
                        $historyUids[]   = $this->event->uid;
                        break;
                    case MainProcessEnum::ENGINEER_CONSTRUCT_CONFIRM_FINISH:
                        //筹备部确认施工完成
                        $engineeringUids = EngineeringModel::query()
                                                           ->where('orderNo', $this->mainProcessModel->orderNo)
                                                           ->where('step',
                                                               MainProcessEnum::ENGINEER_CONSTRUCT_CONFIRM_FINISH)
                                                           ->pluck("updatedByUid")
                                                           ->toArray();
                        $historyUids     = array_merge($engineeringUids, $historyUids);
                        $historyUids[]   = $this->event->uid;
                        break;
                    case MainProcessEnum::SHOP_CHECK_ASSIGN_MANAGER:
                        //指派营运督导
                        $checkAcceptUids = OpenShopCheckAcceptModel::query()
                                                                   ->where('orderNo', $this->mainProcessModel->orderNo)
                                                                   ->where('step',
                                                                       MainProcessEnum::SHOP_CHECK_ASSIGN_MANAGER)
                                                                   ->pluck("managerId")
                                                                   ->toArray();
                        $historyUids     = array_merge($checkAcceptUids, $historyUids);
                        $historyUids[]   = $this->event->uid;
                        break;
                    case MainProcessEnum::SHOP_CHECK_BY_MANAGER:
                        //督导验收&提交表单
                        $checkAcceptUids = OpenShopCheckAcceptModel::query()
                                                                   ->where('orderNo', $this->mainProcessModel->orderNo)
                                                                   ->where('step',
                                                                       MainProcessEnum::SHOP_CHECK_BY_MANAGER)
                                                                   ->pluck("managerId")
                                                                   ->toArray();
                        $historyUids     = array_merge($checkAcceptUids, $historyUids);
                        $historyUids[]   = $this->event->uid;
                        break;
                }
                break;
        }

        $this->insertModel($historyUids);

        if (HistoryOrderParam::$importOrderMode) {

            // 不发
            Log::channel('listener')
               ->info(__CLASS__ . " handle ProcessNoticeListener...orderNo: {$this->mainProcessModel->orderNo} import skip...");

            return;
        }

        //正常发消息
        $this->notice($noticeHistoryUids);
    }

    // 发送通知消息
    protected function notice(array $uids)
    {
        $noticeServer = new NoticeService($this->orderModel->orderNo, $this->event->uid);
        $noticeServer->setHistoryUserList($uids);

        switch ($this->event->mainProcessType) {
            case MainProcessEnum::MAIN_PROCESS_INVESTMENT_SIGN:
                switch ($this->event->processType) {
                    case MainProcessEnum::INVESTMENT_SIGN_CONTRACT_PAID:
                        // 支付，要区分主订单子订单
                        if ($this->mainProcessModel->childOrder == MainProcessEnum::CHILD_ORDER_0) {
                            $payHistory = OpenShopSignOrderPayHistoryModel::query()
                                                                          ->where('orderNo', $this->orderModel->orderNo)
                                                                          ->where('payStatus', 1)
                                                                          ->where('isDeleted', 0)
                                                                          ->get();
                            NoticeService::paid($uids, $this->orderModel, $payHistory);
                        }
                        break;
                    // 签合同
                    case MainProcessEnum::INVESTMENT_SIGN_CONTRACT_SIGNED:
                        NoticeService::signed($uids, $this->orderModel);
                        $noticeServer->setHistoryDealByUserId($noticeServer->getLastSiteMissionInfo()->dealUid ?? 0);
                        NoticeService::signedJob($this->orderModel, $noticeServer->historyDealUserName);
                        break;
                }
                break;
            case MainProcessEnum::MAIN_PROCESS_SHOP_LOCATION:
                switch ($this->event->processType) {
                    // 指派开发经理
                    case MainProcessEnum::SHOP_LOCATION_ASSIGN_MANAGER:
                        $body         = RmsUserService::getRmsUserInfoByUserIds([$this->event->uid]);
                        $operatorName = $body['data'][0]['username'] ?? '';
                        NoticeService::assignManagerJob($this->orderModel, $operatorName);
                        NoticeService::assignManagerHistory($uids, $this->orderModel, $operatorName);
                        break;
                    // 选址报告上传
                    case MainProcessEnum::SHOP_LOCATION_FORM_SUBMIT:
                        NoticeService::locationUploadedHistory($uids, $this->orderModel);
                        break;
                    // 门店已经确认选址
                    case MainProcessEnum::SHOP_LOCATION_CONFIRM:
                        $noticeServer->setHistoryDealByUserId($noticeServer->getLastSiteMissionInfo()->dealUid);
                        NoticeService::locationConfirmed($this->orderModel, $noticeServer->historyDealUserName);
                        NoticeService::locationConfirmedHistory($uids, $this->orderModel,
                            $noticeServer->historyDealUserName);
                        break;
                }
                break;
            case MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN:
                switch ($this->event->processType) {
                    case MainProcessEnum::SPACE_DESIGN_STEP_TYPE_CONFIRM_SHOP_INFO:
                        //筹备确认
                        $noticeServer->setNoticeContent(
                            MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN_MAP[MainProcessEnum::SPACE_DESIGN_STEP_TYPE_CONFIRM_SHOP_INFO]
                        );
                        $noticeServer->spaceDesignJobV2('指派空间设计师');
                        $noticeServer->spaceDesignHistory('筹备确认建店信息', 'preparationName', MissionTemplateEnum::STEP_STATUS_6);
                        break;
                    case MainProcessEnum::SPACE_DESIGN_STEP_TYPE_SPACE_DESIGN_ASSIGN_WAIT:
                        //指定空间设计师
//                        $noticeServer->setNoticeContent(
//                            MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN_MAP[MainProcessEnum::SPACE_DESIGN_STEP_TYPE_SPACE_DESIGN_ASSIGN_WAIT]
//                        );
//                        $noticeServer->setMissionContent('设计师确认设计模块'); // 您有一条任务待处理
//                        $noticeServer->spaceDesignJob();
//
//                        $noticeServer->setHistoryDealByUserId($noticeServer->getLastSiteMissionInfo(true)->dealUid);
//                        $noticeServer->spaceDesignHistory('指定空间设计师');
                        break;
                    case MainProcessEnum::SPACE_DESIGN_STEP_TYPE_DESIGN_CONFIRM_DESIGN_MODULE:
                        //设计师确认设计模块
//                        $noticeServer->setNoticeContent(
//                            MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN_MAP[MainProcessEnum::SPACE_DESIGN_STEP_TYPE_DESIGN_CONFIRM_DESIGN_MODULE]
//                        );
//                        $noticeServer->spaceDesignHistory('设计师确认设计模块');
                        break;
                    case MainProcessEnum::SPACE_DESIGN_STEP_TYPE_DESIGN_MODULE_PAY:


                        // 付钱

                        // 任务完成才推
                        $checkMission = OpenShopSiteMissionModel::query()
                                                                ->where('orderNo', $noticeServer->orderInfo->orderNo)
                                                                ->where('stepStatus', MissionTemplateEnum::STEP_STATUS_14)
                                                                ->where('status', SiteMissionEnum::STATUS_1)
                                                                ->count();
                        if (!$checkMission) {
                            $noticeServer->setNoticeContent(
                                MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN_MAP[MainProcessEnum::SPACE_DESIGN_STEP_TYPE_DESIGN_MODULE_PAY]
                            );
                            $noticeServer->setMissionContent('设计师待确认设备模块');
                            $noticeServer->spaceDesignJob();
                        }

                        $noticeServer->setHistoryUserType('商家');
                        $noticeServer->setHistoryDealByUserId($noticeServer->orderInfo->merchantUserId);
                        $noticeServer->spaceDesignHistory('商家已付款设计模块');


                        //筹备发送设计模块已支付信息
                        $openShopSignOrderPayHistoryFirst = OpenShopSignOrderPayHistoryModel::query()
                                                                                            ->where("orderNo", $noticeServer->orderInfo->orderNo)
                                                                                            ->where("sysType", CheckEnum::SYS_TYPE_OPEN_SHOP_DESIGN)
                                                                                            ->where("payStatus", OpenShopSignOrderPayHistoryModel::PAY_STATUS_OK)
                                                                                            ->orderBy("id", "desc")
                                                                                            ->first();
                        $payAmount = (int) ($openShopSignOrderPayHistoryFirst ? $openShopSignOrderPayHistoryFirst->payPrice : 0);

                        $preparationUid = OpenShopSpaceDesignModel::query()->where("orderNo", $noticeServer->orderInfo->orderNo)->first()->preparationUid;
                        if($preparationUid && ($preparationRmsUser = RmsUserRemote::getRmsUserByIds([$preparationUid]))){
                            $preparationName = empty($noticeServer->historyDealUserName) ? $noticeServer->orderInfo->spaceDesignRel->designName : $noticeServer->historyDealUserName;
                            FeishuMsgRemote::getInstance()->batchSendPostMsg(
                                TextMsgBuildTools::getInstance("设计模块已支付")->setRowContent(
                                    new TextTagMsgBuild("门店编号:" .((string)$noticeServer->orderInfo->shopCode).PHP_EOL),
                                    new TextTagMsgBuild("门店名称:" .$noticeServer->getShopName().PHP_EOL),
                                    new TextTagMsgBuild("支付金额:" .formatPrice($payAmount).PHP_EOL),
                                    new TextTagMsgBuild("处理时间:" .$noticeServer->orderInfo->spaceDesignRel->updatedAt->toDateString().PHP_EOL),
                                    new TextTagMsgBuild("处理人:" .$preparationName.PHP_EOL.PHP_EOL),
                                )->setRowContent(
                                    new ImgTagMsgBuild("img_v2_3a16f5bb-6f56-4822-8e2f-c41a420e809g", 300, 200)
                                )->toArray(), collect($preparationRmsUser)->pluck("feishuUserId")->toArray());
                        }
                        break;
                    case MainProcessEnum::SPACE_DESIGN_STEP_TYPE_DESIGN_CONFIRM_EQUIPMENT_MODULE:
                        //设计师确认设备模块
                        $noticeServer->setNoticeContent(
                            MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN_MAP[MainProcessEnum::SPACE_DESIGN_STEP_TYPE_DESIGN_CONFIRM_EQUIPMENT_MODULE]
                        );

                        $noticeServer->spaceDesignHistory('设计师已确认设备模块');
                        break;
                    case MainProcessEnum::SPACE_DESIGN_STEP_TYPE_DESIGN_EQUIPMENT_PAY:

                        //设计师确认设备模块
                        // 任务完成才推
                        $checkMission = OpenShopSiteMissionModel::query()
                                                                ->where('orderNo', $noticeServer->orderInfo->orderNo)
                                                                ->where('stepStatus', MissionTemplateEnum::STEP_STATUS_16)
                                                                ->where('status', SiteMissionEnum::STATUS_1)
                                                                ->count();
                        if (!$checkMission) {

                            $noticeServer->setNoticeContent(
                                MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN_MAP[MainProcessEnum::SPACE_DESIGN_STEP_TYPE_DESIGN_EQUIPMENT_PAY]
                            );
                            $noticeServer->setMissionType('审核');
                            $noticeServer->setMissionContent('设计师提交整套图');
                            $noticeServer->spaceDesignJob();
                        }


                        $noticeServer->setHistoryUserType('商家');
                        $noticeServer->setHistoryDealByUserId($noticeServer->orderInfo->merchantUserId);
                        $noticeServer->spaceDesignHistory('商家已付款设备模块');

                        //筹备发送设备模块已支付信息
                        $openShopSignOrderPayHistoryFirst = OpenShopSignOrderPayHistoryModel::query()
                                                                                         ->where("orderNo", $noticeServer->orderInfo->orderNo)
                                                                                         ->where("sysType", CheckEnum::SYS_TYPE_OPEN_SHOP_EQUIPMENT)
                                                                                         ->where("payStatus", OpenShopSignOrderPayHistoryModel::PAY_STATUS_OK)
                                                                                         ->orderBy("id", "desc")
                                                                                         ->first();
                        $payAmount = (int) ($openShopSignOrderPayHistoryFirst ? $openShopSignOrderPayHistoryFirst->payPrice : 0);

                        $preparationUid = OpenShopSpaceDesignModel::query()->where("orderNo", $noticeServer->orderInfo->orderNo)->first()->preparationUid;
                        if($preparationUid && ($preparationRmsUser = RmsUserRemote::getRmsUserByIds([$preparationUid]))){
                            $preparationName = empty($noticeServer->historyDealUserName) ? $noticeServer->orderInfo->spaceDesignRel->designName : $noticeServer->historyDealUserName;
                            FeishuMsgRemote::getInstance()->batchSendPostMsg(
                                TextMsgBuildTools::getInstance("设备模块已支付")->setRowContent(
                                    new TextTagMsgBuild("门店编号:" .((string)$noticeServer->orderInfo->shopCode).PHP_EOL),
                                    new TextTagMsgBuild("门店名称:" .$noticeServer->getShopName().PHP_EOL),
                                    new TextTagMsgBuild("支付金额:" .formatPrice($payAmount).PHP_EOL),
                                    new TextTagMsgBuild("处理时间:" .$noticeServer->orderInfo->spaceDesignRel->updatedAt->toDateString().PHP_EOL),
                                    new TextTagMsgBuild("处理人:" .$preparationName.PHP_EOL.PHP_EOL),
                                )->setRowContent(
                                    new ImgTagMsgBuild("img_v2_6ad564c3-1367-465a-badf-2ed16e4aa6eg", 300, 200)
                                )->toArray(), collect($preparationRmsUser)->pluck("feishuUserId")->toArray());
                        }
                        break;
                    case MainProcessEnum::SPACE_DESIGN_STEP_TYPE_DESIGN_UPLOAD_FILE:
                        //上传设计图
                        $noticeServer->setNoticeContent(
                            MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN_MAP[MainProcessEnum::SPACE_DESIGN_STEP_TYPE_DESIGN_UPLOAD_FILE]
                        );

                        $noticeServer->spaceDesignHistory('上传空间设计图');
                        break;
                    case MainProcessEnum::SPACE_DESIGN_STEP_TYPE_SPACE_FINISH:
                        //上传设计图
                        // 任务完成才推
                        $checkMission = OpenShopSiteMissionModel::query()
                                                                ->where('orderNo', $noticeServer->orderInfo->orderNo)
                                                                ->where('stepStatus', MissionTemplateEnum::STEP_STATUS_17)
                                                                ->where('status', SiteMissionEnum::STATUS_1)
                                                                ->count();
                        if (!$checkMission) {
                            $noticeServer->setNoticeContent(
                                MainProcessEnum::MAIN_PROCESS_SPACE_DESIGN_MAP[MainProcessEnum::SPACE_DESIGN_STEP_TYPE_SPACE_FINISH]
                            );
                            $noticeServer->setMissionType('审核');
                            $noticeServer->setMissionContent('筹备部确认施工进场');
                            $noticeServer->spaceDesignJob();
                        }



                        $noticeServer->setHistoryUserType('商家');
                        $noticeServer->setHistoryDealByUserId($noticeServer->orderInfo->merchantUserId);
                        $noticeServer->spaceDesignHistory('商家已付款软装支付');

                        //筹备发送软装模块已支付信息
                        $openShopSignOrderPayHistoryFirst = OpenShopSignOrderPayHistoryModel::query()
                                                                                            ->where("orderNo", $noticeServer->orderInfo->orderNo)
                                                                                            ->where("sysType", CheckEnum::SYS_TYPE_OPEN_SHOP_DECORATE)
                                                                                            ->where("payStatus", OpenShopSignOrderPayHistoryModel::PAY_STATUS_OK)
                                                                                            ->orderBy("id", "desc")
                                                                                            ->first();
                        $payAmount =  (int) ($openShopSignOrderPayHistoryFirst ? $openShopSignOrderPayHistoryFirst->payPrice : 0);

                        $preparationUid = OpenShopSpaceDesignModel::query()->where("orderNo", $noticeServer->orderInfo->orderNo)->first()->preparationUid;
                        if($preparationUid && ($preparationRmsUser = RmsUserRemote::getRmsUserByIds([$preparationUid]))){
                            $preparationName = empty($noticeServer->historyDealUserName) ? $noticeServer->orderInfo->spaceDesignRel->designName : $noticeServer->historyDealUserName;
                            FeishuMsgRemote::getInstance()->batchSendPostMsg(
                                TextMsgBuildTools::getInstance("软装模块已支付")->setRowContent(
                                    new TextTagMsgBuild("门店编号:" .((string)$noticeServer->orderInfo->shopCode).PHP_EOL),
                                    new TextTagMsgBuild("门店名称:" .$noticeServer->getShopName().PHP_EOL),
                                    new TextTagMsgBuild("支付金额:" .formatPrice($payAmount).PHP_EOL),
                                    new TextTagMsgBuild("处理时间:" .$noticeServer->orderInfo->spaceDesignRel->updatedAt->toDateString().PHP_EOL),
                                    new TextTagMsgBuild("处理人:" .$preparationName.PHP_EOL.PHP_EOL),
                                )->setRowContent(
                                    new ImgTagMsgBuild("img_v2_696bf696-e4a1-4d3b-abb6-228459c1161g", 300, 200)
                                )->toArray(), collect($preparationRmsUser)->pluck("feishuUserId")->toArray());
                        }
                        break;
                }
                break;
            case MainProcessEnum::MAIN_PROCESS_ENGINEER_CONSTRUCT:
                switch ($this->event->processType) {
                    case MainProcessEnum::ENGINEER_CONSTRUCT_START:
                        //筹备部确认施工进场
                        $body         = RmsUserService::getRmsUserInfoByUserIds([$this->event->uid]);
                        $operatorName = $body['data'][0]['username'] ?? '';
                        NoticeService::engineeringStart($uids, $this->orderModel, $operatorName);
                        NoticeService::engineeringStartJob($this->orderModel, $operatorName);
                        break;
                    case MainProcessEnum::ENGINEER_CONSTRUCT_CONFIRM_FINISH:
                        //筹备部确认施工完成
                        $body         = RmsUserService::getRmsUserInfoByUserIds([$this->event->uid]);
                        $operatorName = $body['data'][0]['username'] ?? '';
                        NoticeService::engineeringOk($uids, $this->orderModel, $operatorName);
                        if ($this->mainProcessModel->step == MainProcessEnum::MAIN_PROCESS_ENGINEER_CONSTRUCT) {
                            NoticeService::engineeringOkJob($this->orderModel, $operatorName);
                        }
                        break;
                }
                break;
            case MainProcessEnum::MAIN_PROCESS_SHOP_CHECK :
                // 获取原来的流程
//                if ($this->mainProcessModel->step == MainProcessEnum::MAIN_PROCESS_SHOP_CHECK) {
                switch ($this->event->processType) {
                    case MainProcessEnum::SHOP_CHECK_ASSIGN_MANAGER:
                        //指派营运督导
                        $body         = RmsUserService::getRmsUserInfoByUserIds([$this->event->uid]);
                        $operatorName = $body['data'][0]['username'] ?? '';
                        NoticeService::acceptAssignManager($this->orderModel, $operatorName);
                        NoticeService::acceptAssignManagerHistory($uids, $this->orderModel, $operatorName);
                        break;
//                    case MainProcessEnum::SHOP_CHECK_BY_MANAGER:
//                        //督导验收&提交表单
//                        $body         = RmsUserService::getRmsUserInfoByUserIds([$this->event->uid]);
//                        $operatorName = $body['data'][0]['username'] ?? '';
//                        NoticeService::managerConfirmSubmit($uids, $this->orderModel, $operatorName);
//                        $noticeServer->setMissionType('审核');
//                        $noticeServer->setMissionContent('开业表单待提交');
//                        $noticeServer->startBusinessJob();
//                        break;
//                    }
                }
                break;
            // 开业报告
            case MainProcessEnum::MAIN_PROCESS_START_BUSINESS:
                switch ($this->event->processType) {
                    // 表单待提交
                    case MainProcessEnum::START_BUSINESS_STAY:
                        $noticeServer->startBusinessHistory('开业表单已经提交.');
                        break;
                    // 开业表单提交完成
                    case MainProcessEnum::START_BUSINESS_SUBMIT:
                        $noticeServer->startBusinessHistory('开业表单已经提交!');
                        break;
                }
                break;
        }
    }

    protected function insertModel(array $arr)
    {
        $arr                   = array_unique($arr);
        $m                     = OpenShopProcessDetailNoticeEmployeeModel::firstOrNew([
            'orderNo'     => $this->orderModel->orderNo,
            'step'        => $this->mainProcessModel->step,
            'processType' => $this->event->processType,
        ]);
        $m->noticeEmployeeUids = $arr;
        $m->save();
    }
}
